//
//  shopView.m
//  tcpoctest
//
//  Created by apple on 13-7-3.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "shopView.h"

@implementation shopView

-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = @"商城";
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回" target:self action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    
    mywebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,
                                                          TOPBAR_HIGHT,
                                                          WINDOW_WIDTH,
                                                          WINDOW_HIGTH - TOPBAR_HIGHT)];
    
    mywebView.scalesPageToFit = YES;
    mywebView.delegate =self;
    
    [self.view addSubview:mywebView];
}

#pragma mark -private fun-
-(void)backButtonPressed
{
    [[ViewManager sharedViewManager] backView:self];
}

-(void)viewWillShow
{
    NSString *mail = [DataControl shareControl].personDS.uinfo.mail;
    if (mail != nil && ![mail isEqualToString:@""]) {
        
        NSString *strURL = @"http://shop.peng7.net/mobile.html";
        NSURL *url =[NSURL URLWithString:strURL];
        NSURLRequest *request =[NSURLRequest requestWithURL:url];
        [mywebView loadRequest:request];
    }
}

#pragma mark -web delegat-
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType
{
    static bool bpost = true;
    
    if ([request.URL.absoluteString rangeOfString:@"orderdis"].length != 0) {
        
        if (bpost) {
            
            NSURL *url = [NSURL URLWithString:request.URL.absoluteString];
            
            
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
            
            [request setHTTPMethod:@"POST"];
            
            NSString *str = [NSString stringWithFormat:@"USER=%d&PWD=%@",
                             [DataControl shareControl].personDS.uinfo.ppNumber
                             ,[DataControl shareControl].personDS.uinfo.ps];
            
            NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
            
            [request setHTTPBody:data];
            
            [mywebView loadRequest:request];
            
            bpost = false;
            
            return NO;
        }
        else
        {
            bpost = true;
            
            return YES;
        }
    }
    else
    {
        return YES;
    }
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"网页打开有问题耶"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:nil
                                              otherButtonTitles:@"OK", nil];
    [alterview show];
}

@end
